home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Text⁄Files / Writeswell Jr. 1.0.2 Master / WSI Library Source / AppEvents.c < prev    next >
C/C++ Source or Header  |  1992-04-19  |  4KB  |  183 lines

  1. #include <AppleEvents.h>
  2. #include "AppEventDefs.h"
  3. #include "AppEvents.h"
  4. #include "MyHandlers.h"
  5. #include "TestBed.h"
  6. #include "Gripe.h"
  7.  
  8. static StringPtr gServiceName;
  9.  
  10. OSErr InitReqHandlers( void )
  11. {
  12.     OSErr err;
  13.  
  14.     if ( err = AEInstallEventHandler( kCoreEventClass,
  15.                                 kAEOpenApplication,
  16.                                 (EventHandlerProcPtr)MyOAPPHandler,
  17.                                 0,
  18.                                 false ) ){
  19.         Gripe( "\poapp install failed" );
  20.         return err;
  21.     }
  22.  
  23.     if ( err = AEInstallEventHandler( kCoreEventClass,
  24.                                 kAEOpenDocuments,
  25.                                 (EventHandlerProcPtr)MyODocHandler,
  26.                                 0,
  27.                                 false ) ){
  28.         Gripe( "\podoc install failed" );
  29.         return err;
  30.     }
  31.     
  32.     if ( err = AEInstallEventHandler( kCoreEventClass,
  33.                                 kAEPrintDocuments,
  34.                                 (EventHandlerProcPtr)MyPDocHandler,
  35.                                 0,
  36.                                 false ) ){
  37.         Gripe( "\ppdoc install failed" );
  38.         return err;
  39.     }
  40.     
  41.     if ( err = AEInstallEventHandler( kCoreEventClass,
  42.                                 kAEQuitApplication,
  43.                                 (EventHandlerProcPtr)MyQuitHandler,
  44.                                 0,
  45.                                 false ) ){
  46.         Gripe( "\pquit install failed" );
  47.         return err;
  48.     }
  49.  
  50.     if ( err = AEInstallEventHandler( kCoreEventClass,
  51.                                 kAEAnswer,
  52.                                 (EventHandlerProcPtr)MyAnswerHandler,
  53.                                 0,
  54.                                 false ) ){
  55.         Gripe( "\pAnswer install failed" );
  56.         return err;
  57.     }
  58.  
  59.     return noErr;
  60. }
  61.  
  62. /* This gets a connection using the PPC browser.  From IM VI p. 6-58 */
  63.  
  64. OSErr GetTargetAddress( StringPtr myPrompt,
  65.                         StringPtr myAppStr,
  66.                         PortInfoRec *myPortInfoPtr,
  67.                         AEAddressDesc *targetAddressPtr,
  68.                         StringPtr serviceName,
  69.                         TargetID *toTargetIDPtr )
  70. {
  71.     OSErr err;
  72.     
  73.     gServiceName = serviceName;
  74.  
  75.     err = PPCBrowser( myPrompt,
  76.                         myAppStr,
  77.                         false,
  78.                         &( toTargetIDPtr->location ),
  79.                         myPortInfoPtr,
  80.                         (PPCFilterProcPtr)MyPPCBrowseProc,
  81.                         (StringPtr)"\p" );
  82.     if ( err )
  83.         return err;
  84.     
  85.     BlockMove( &(myPortInfoPtr->name),
  86.                 &(toTargetIDPtr->name),
  87.                 sizeof(myPortInfoPtr->name) );
  88.     
  89.     err = AECreateDesc( typeTargetID,
  90.                         (Ptr)toTargetIDPtr,
  91.                         sizeof( *toTargetIDPtr ), 
  92.                         targetAddressPtr );
  93.     
  94.     return err;
  95. }
  96.  
  97. pascal Boolean MyPPCBrowseProc( LocationNamePtr theLoc, PortInfoPtr thePortInfo )
  98. {
  99.     return true;
  100. #ifdef NEVER
  101.     if ( thePortInfo->name.portKindSelector == ppcByString ){
  102.         if ( PStrCmp( thePortInfo->name.u.portTypeStr, (StringPtr)"\pWORDSERVICES" ) )
  103.             return true;
  104.         else
  105.             return false;
  106.     }
  107.     return false;
  108. #endif
  109. }
  110.  
  111. Boolean PStrCmp( StringPtr foo, StringPtr bar )
  112. {
  113.     short len;
  114.     
  115.     len = (short)( foo[ 0 ] );
  116.     
  117.     len += 1;            /* include the length byte itself */
  118.  
  119.     while ( len-- )
  120.         if ( *foo++ != *bar++ )
  121.             return false;
  122.  
  123.     return true;
  124. }
  125.  
  126. /* GetEventType is a utility routine to get the event class and ID out of an apple event */
  127.  
  128. OSErr GetEventType( AppleEvent *theAppleEventPtr,
  129.                     AEEventClass *theClassPtr,
  130.                     AEEventID *theIDPtr )
  131. {
  132.     OSErr        err;
  133.     Size        realSize;
  134.     DescType    realType;
  135.     
  136.     /* Get the event class */
  137.     
  138.     err = AEGetAttributePtr( theAppleEventPtr,
  139.                                 keyEventClassAttr,
  140.                                 typeType,
  141.                                 &realType,
  142.                                 (Ptr)theClassPtr,
  143.                                 (Size)sizeof( AEEventClass ),
  144.                                 &realSize );
  145.     
  146.     if ( err )
  147.         return err;
  148.  
  149.     /* Get the event ID */
  150.     
  151.     err = AEGetAttributePtr( theAppleEventPtr,
  152.                                 keyEventIDAttr,
  153.                                 typeType,
  154.                                 &realType,
  155.                                 (Ptr)theIDPtr,
  156.                                 (Size)sizeof( AEEventID ),
  157.                                 &realSize );
  158.     
  159.     
  160.     return err;
  161. }
  162.  
  163. OSErr GetEventID( AppleEvent *theAppleEventPtr,
  164.                     AEEventID *theIDPtr )
  165. {
  166.     OSErr        err;
  167.     Size        realSize;
  168.     DescType    realType;
  169.     
  170.     /* Get the event ID */
  171.     
  172.     err = AEGetAttributePtr( theAppleEventPtr,
  173.                                 keyEventIDAttr,
  174.                                 typeType,
  175.                                 &realType,
  176.                                 (Ptr)theIDPtr,
  177.                                 (Size)sizeof( AEEventID ),
  178.                                 &realSize );
  179.     
  180.     
  181.     return err;
  182. }
  183.